﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class PurchaseController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IEmployeeRepository employeeRepository;
        private readonly ICurrencyRepository currencyRepository;
        private readonly IStockRepository stockRepository;
        private readonly IContactRepository contactRepository;
        private readonly IPurchaseRepository purchaseRepository;
        private readonly ICompanyRepository companyRepository;
        private readonly IMaterialRepository materialRepository;
        private readonly IUOMRepository uomRepository;


        public PurchaseController(ICommandBus commandBus, IPurchaseRepository purchaseRepository, ICompanyRepository companyRepository, IEmployeeRepository employeeRepository, ICurrencyRepository currencyRepository, IStockRepository stockRepository, IContactRepository contactRepository, IMaterialRepository materialRepository, IUOMRepository uomRepository)
        {
            ModelState.Clear();
            this.commandBus = commandBus;
            this.purchaseRepository = purchaseRepository;
            this.companyRepository = companyRepository;
            this.employeeRepository = employeeRepository;
            this.currencyRepository = currencyRepository;
            this.stockRepository = stockRepository;
            this.contactRepository = contactRepository;
            this.materialRepository = materialRepository;
            this.uomRepository = uomRepository;
        }
        //
        // GET: /Purchase/
        [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = purchaseRepository.GetAll();
            ViewData["Company"] = companyRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }

        public ActionResult Create(int? ID)
        {
            ViewData["Currency"] = currencyRepository.GetAll();
            ViewData["Employee"] = employeeRepository.GetAll();            
            ViewData["Warehouse"] = stockRepository.GetAll();
            ViewData["Contact"] = contactRepository.GetAll();
            ViewData["UOM"] = uomRepository.GetAll();
            ViewData["Material"] = materialRepository.GetAll();

            var purchase = purchaseRepository.GetById(ID ?? 0);
            var purchaseForms = Mapper.Map<Purchase, PurchaseFormModels>(purchase);
            if (purchase != null)
            {
            }
            return View(purchaseForms);
        }
        [HttpPost]
        public ActionResult Save(PurchaseFormModels frm)
        {
            var form = new PurchaseFormModels()
            {
                ID = frm.ID,
                ApprovalDate=frm.ApprovalDate,
                ApprovalUserID=frm.ApprovalUserID,
                Attactment=frm.Attactment,
                Code=frm.Code,
                CompanyID=frm.CompanyID,
                ContactAddress=frm.ContactAddress,
                ContactID=frm.ContactID,
                ContactName=frm.ContactName,
                CreatedDate=DateTime.Now,
                CreatedUserID=frm.CreatedUserID,
                Discount=frm.Discount,
                DueDate=frm.DueDate,
                Note=frm.Note,
                Status=frm.Status,
                StockID=frm.StockID,
                SubTotal=frm.SubTotal,
                Tax=frm.Tax,
                TotalMoney=frm.TotalMoney,
                UseVAT=frm.UseVAT
            };
            var command = Mapper.Map<PurchaseFormModels, CreateOrUpdatePurchaseCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);

            }
            var model = purchaseRepository.GetAll();
            return PartialView("_PurchaseList", model.ToPagedList(1, 10));
        }

        [HttpGet]
        public JsonResult Get(int ID)
        {
            Purchase product = purchaseRepository.GetById(ID);
            return Json(product, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeletePurchaseCommand> list = new List<DeletePurchaseCommand>();
            foreach (var item in ProID)
            {
                var command = new DeletePurchaseCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = purchaseRepository.GetAll();
            return PartialView("_PurchaseList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<Purchase> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = purchaseRepository.GetAll();
            }
            else
            {
                model = purchaseRepository.GetMany(x => x.Code.Contains(Name));

            }
            return PartialView("_PurchaseList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = purchaseRepository.GetAll();
            return PartialView("_PurchaseList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
